<?php
/**
 * Created by PhpStorm.
 * User: wttfire
 * Date: 2021/1/29
 * Time: 下午3:16
 */
namespace app\api\model;

use app\api\controller\Common;
use think\db\Expression;
use think\Exception;
use think\Model;

class SupplierTravelGuides extends Model{


    //初始化
    public function __construct()
    {
        parent::__construct();
    }

    //更新数据
    public function createGuide($post)
    {
        $post['create_time'] = time();
        $this->startTrans();
        try{
            $this->allowField(true)->save($post);
            $this->commit();
            $post['id'] = $this->id;
            return $post;
        }catch (\Exception $e){
            $this->rollback();
            return $e->getMessage();
        }
    }

    //更新数据
    public function saveGuide($id,$post)
    {
        $post['update_time'] = time();
        $this->startTrans();
        try{
            $this->allowField(true)->save($post,['id'=>$id]);
            $this->commit();
            return $post;
        }catch (\Exception $e){
            $this->rollback();
            return $e->getMessage();
        }
    }

    //删除数据
    public function deleteGuide($id)
    {
        $info = $this->where(['id'=>$id])->find();
        if(empty($info)) return [];
        $post['is_delete'] = 1;
        $post['delete_time'] = time();
        $this->startTrans();
        try{
            $this->where(['id'=>$id])->update($post);
            $this->commit();
            return $post;
        }catch (\Exception $e){
            $this->rollback();
            return $e->getMessage();
        }
    }

    //读取详情
    public function infoGuide($id,$supplier_admin_ids=[])
    {
        $info = db('supplier_travel_guides')
//            ->where(['supplier_admin_id'=>['in',$supplier_admin_ids]])
            ->find($id);
        if(empty($info)) return [];
        if(!empty($info['link_poi_ids'])){
            $info['poies'] = db('poi')->where(['id'=>['in',$info['link_poi_ids']]])
                ->field('id,name,eng_name,groupid,introduce,lat,lng,thumb,address,create_time,update_time')
                ->order(new Expression('field(id,'.$info['link_poi_ids'].')'))
                ->select();
        }else{
            $info['poies'] = [];
        }
        $info['categoryName'] = $this->getCategooryTypeNameById($info['category']);
        $info['content'] = htmlspecialchars_decode($info['content']);
        $info['tagsArr'] = explode(',',$info['tags']);
        $info['create_time'] = date('Y-m-d H:i:s',$info['create_time']);
        $info['update_time'] = date('Y-m-d H:i:s',$info['update_time']);
        return $info;
    }

    //读取列表
    public function listGuide($where,$page,$pageLimit)
    {
        $count = db('supplier_travel_guides')->where($where)->count();
        $list = db('supplier_travel_guides')->where($where)
            ->field('id,title,description,create_time,update_time,cover_img,
                supplier_admin_id,link_poi_ids,sort_num,browse,fake_browse,category')
            ->order('sort_num desc,create_time desc')
            ->page($page,$pageLimit)->select();

        if(empty($list)) return [];
        foreach ($list as $key=>$value){
            if(!empty($value['link_poi_ids'])){
                $list[$key]['poies'] = db('poi')->where(['id'=>['in',$value['link_poi_ids']]])
                    ->field('id,name,eng_name,groupid,introduce,lat,lng,thumb,address')
                    ->order(new Expression('field(id,'.$value['link_poi_ids'].')'))
                    ->select();
            }else{
                $list[$key]['poies'] = [];
            }
            $list[$key]['categoryName'] = $this->getCategooryTypeNameById($value['category']);
            $list[$key]['show_browse'] = $value['browse'] + $value['fake_browse'];
            $list[$key]['supplier_name'] = (new Common())->getSupplierAdminInfo($value['supplier_admin_id'])['supplier_name'] ?? '';
            $list[$key]['create_time'] = date('Y-m-d H:i:s',$value['create_time']);
            $list[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
        }

        $result = [
            'totalCount'    => $count,
            'totalPage'     => ceil($count/$pageLimit),
            'page'          => $page,
            'pageLimit'     => $pageLimit,
            'list'          => $list
        ];
        return $result;
    }

    public function getCategoryType(){
        return [
            '旅行宝典'=>1,
            '全域导览'=>2,
        ];
    }
    public function getCategooryTypeNameById($category=1)
    {
        $categoryData = $this->getCategoryType();
        $categoryData = array_flip($categoryData);
        return $categoryData[$category];
    }
}